package pl.jug.trojmiasto.luceneExample;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.function.CustomScoreQuery;
import org.apache.lucene.search.function.FieldScoreQuery;
import org.apache.lucene.search.function.FieldScoreQuery.Type;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class SearchExample {
	public static void main(String[] args) throws IOException, ParseException {

		Directory dir = FSDirectory.open(new File("/tmp/jug/images-index"));

		IndexSearcher searcher = new IndexSearcher(IndexReader.open(dir));

		QueryParser parser = new QueryParser(Version.LUCENE_36, "nazwa",
				new StandardAnalyzer(Version.LUCENE_36));

		Query wordQuery = parser.parse("lucene*");
		
		
		
		CustomScoreQuery csQuery = new RatioBoostingQuery(wordQuery, 1.5f, 2.0f, "proporcja");
		
		TermQuery query = new TermQuery(new Term("nazwa", "lucene"));
		
		System.out.println(searcher.explain(query, 0));
		
		TopDocs result = searcher.search(query, 3);
		
		
		System.out.println("Zaleziono " + result.totalHits + " wyników");
		ScoreDoc[] docs = result.scoreDocs;
		for (ScoreDoc scoreDoc : docs) {
			int docID = scoreDoc.doc;

			Document doc = searcher.doc(docID);

			System.out.println(scoreDoc.score + " "
					+ doc.getFieldable("nazwa").stringValue() + ":"
					+ doc.getFieldable("proporcja").stringValue());
		}
	}
}
